package com.ruge.system.service.impl;

import com.ruge.common.util.GsonUtil;
import com.ruge.system.pojo.entity.Notice;
import com.ruge.system.repository.NoticeRepository;
import com.ruge.system.pojo.request.NoticeRequest;
import com.ruge.system.service.SysNoticeService;
import com.ruge.system.pojo.vo.NoticeVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import java.util.List;
import java.util.Map;

/**
 * @author ：爱丽丝、如歌
 * @time ：2020/3/12  21:54
 * 描述 ：
 */
@Service
public class NoticeServiceImpl implements SysNoticeService {
    @Resource
    private NoticeRepository noticeRepository;

    /**
     * 数据查询接口
     *
     * @param model Notice
     * @return NoticeVo
     */
    @Override
    public Page<NoticeVo> findAll(NoticeRequest model) {
        Specification<Notice> specification = (root, query, cb) -> {
            /**
             * 获取条件对象
             */
            Predicate conjunction = cb.conjunction();
            if (model != null) {
                if (StringUtils.isNotBlank(model.getTitle())) {
                    conjunction.getExpressions().add(cb.like(root.get("title"), "%" + model.getTitle() + "%"));
                }
                if (StringUtils.isNotBlank(model.getCreateBy())) {
                    conjunction.getExpressions().add(cb.like(root.get("createBy"), "%" + model.getCreateBy() + "%"));
                }
                if (null != model.getStartTime()) {
                    conjunction.getExpressions().add(cb.greaterThanOrEqualTo(root.get("lastUpdateTime"), model.getStartTime()));
                }
                if (null != model.getEndTime()) {
                    conjunction.getExpressions().add(cb.lessThanOrEqualTo(root.get("lastUpdateTime"), model.getEndTime()));
                }
            }
            return conjunction;
        };
        Sort sort = Sort.by(Sort.Order.desc("id"));
        Pageable pageable = PageRequest.of(model != null ? model.getPage() : 0, model != null ? model.getLimit() : 15, sort);
        Page<Notice> page = noticeRepository.findAll(specification, pageable);
        return new PageImpl<>(GsonUtil.GsonToListVo(page.getContent(), NoticeVo.class), pageable, page.getTotalElements());
    }

    /**
     * 数据保存接口
     *
     * @param model Notice
     * @return Notice
     */
    @Override
    public Notice save(Notice model) {
        return noticeRepository.save(model);
    }

    /**
     * 数据删除接口
     *
     * @param id id
     */
    @Override
    public void deleteById(Long id) {
        noticeRepository.deleteById(id);
    }

    /**
     * 批量操作接口
     *
     * @param map Map<String, List<SysLogLogin>> map
     * @return List<SysLogLogin>
     */
    @Override
    public List<Notice> batch(Map<String, List<Notice>> map) {
        return noticeRepository.batch(map);
    }
}
